//
//  SeachAskVc.m
//  Qyer
//
//  Created by dllo on 15/10/24.
//  Copyright (c) 2015年 GF. All rights reserved.
//

#import "SeachAskVc.h"

@interface SeachAskVc ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, strong)UISearchBar *searchBar;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, assign)CGFloat cellHeight;
@property (nonatomic, assign)BOOL isSearch;
@end

@implementation SeachAskVc
- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated {
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //[self creatCustomNavBar];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _dataArr = [[NSMutableArray alloc] init];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.hidesBackButton = YES;
    NSLog(@"%@", self.navigationController.navigationBar.subviews);
    for (UIView *view in self.navigationController.navigationBar.subviews) {
        if ([view isKindOfClass:[UISearchBar class]]) {
            _searchBar = (UISearchBar *)view;
            break;
        }
    }
    _searchBar.placeholder = @"搜索问题和答案";
    _searchBar.delegate = self;
    [_searchBar becomeFirstResponder];
    _searchBar.showsCancelButton = YES;
    [self creatSearchTable];
}
#pragma mark 点击输入框
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    _searchBar.showsCancelButton = YES;
    for(id cancel in [_searchBar.subviews[0] subviews]){
        if ([cancel isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)cancel;
            [btn setTitle:@"取消" forState:UIControlStateNormal];
            [btn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        }
    }
    NSLog(@"即将编辑");
#pragma mark 取消按钮样式
    
    return YES;
}
#pragma mark 即将输入
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    NSLog(@"即将输入");
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
}
#pragma mark 输入信息改变时
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    // 搜索提示 自定义type = 1;
}
#pragma mark 点击搜索
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    self.page = 1;
    _isSearch = YES;
    [self getDataWithStr:searchBar.text];
    [GiFHUD show];
    [searchBar resignFirstResponder];
}

#pragma mark 取消
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    searchBar.text = nil;
    _searchBar.showsCancelButton = NO;
    if (![_searchBar resignFirstResponder]) {
        [_searchBar resignFirstResponder];
    }
    [self.navigationController popViewControllerAnimated:YES];
    _searchBar = nil;
}
- (void)creatSearchTable {
    
    _searchTable = [[UITableView alloc] initWithFrame:CRM(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _searchTable.dataSource = self;
    _searchTable.delegate = self;
    _searchTable.backgroundColor = DAY_BACKGROUND;
    [self.view addSubview:_searchTable];
    
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    _searchTable.tableFooterView = view;
}
- (void)getDataWithStr:(NSString *)str {
    if ([Reachability isConnectedInternet]) {
        [AFN getdataWithString:[NSString stringWithFormat:@"http://open.qyer.com/qyer/ask/question/search_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&keyword=%@&count=10&page=%ld", str, self.page] block:^(id block) {
            if (_isSearch == YES) {
                [_dataArr removeAllObjects];
            }
            for (NSDictionary *dic in [block objectForKey:@"data"]) {
                AskModel *model = [AskModel modelWithDictionary:dic];
                [_dataArr addObject:model];
            }
            if (_searchTable.legendFooter == nil) {
                WS(weakSelf);
                [_searchTable addLegendFooterWithRefreshingBlock:^{
                    weakSelf.page = weakSelf.page + 1;
                    _isSearch = NO;
                    [weakSelf getDataWithStr:str];
                }];
            }
            [_searchTable.legendFooter endRefreshing];
            [_searchTable reloadData];
            [GiFHUD dismiss];
        } BLOCK:^(id BLOCK) {
            [GiFHUD dismiss];
        }];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cell11";
    AskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[AskTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    AskModel *model = [_dataArr objectAtIndex:indexPath.row];
    [cell setCellWithModel:model text:_searchBar.text];
    self.cellHeight = cell.height;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.cellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AskModel *model = [_dataArr objectAtIndex:indexPath.row];
    CommunityWeb *web = [[CommunityWeb alloc] init];
    web.urlStr = model.appview_url;
    [self.navigationController pushViewController:web animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
